package object;



public class MyArrayList {
    private String[] data;
    private int size;

    public MyArrayList() {
        data = new String[10];
    }

    public MyArrayList(int capacity) {
        if(capacity <= 10){
            capacity = 10;
        }
        data = new String[capacity];
    }
    public void revise(){
        String[] newData = new String[data.length + (data.length >> 1)];
        for(int i=0;i< data.length;i++){
            newData[i] = data[i];
        }
        data = newData;
    }

    public void add(String elem){
        if(size >= data.length){
            revise();
        }
        data[size] = elem;
        size++;

    }

    public void add(int index,String elem){
        if(index < 0 || index > size){
            throw new ArrayIndexOutOfBoundsException("输入的下标错误");
        }
        if(size >= data.length){
            revise();
        }
        for(int i=size-1;i >=index;i--){
            data[i+1] = data[i];
        }
        data[index] = elem;
        size++;

    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for(int i=0;i<size;i++){
            stringBuilder.append(data[i]);
            if(i < size - 1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    public String remove(int index){
        if(index < 0 || index >= size){
            throw new ArrayIndexOutOfBoundsException("index: " + index + ",size: " + size);
        }
        String str = data[index];
        for(int i=index;i<size-1;i++){
            data[i] = data[i+1];
        }
        size--;
        return str;
    }

    public static void test(){
        MyArrayList list = new MyArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.remove(1);
        System.out.println(list);
    }

    public boolean remove(String elem){
        int i = 0;
        for(;i<size;i++){
            if(data[i].equals(elem))
            break;
        }
        if(i == size){
            return false;
        }
        for(int j=i;j<size -1;j++){
            data[j] = data[j+1];
        }
        size--;
        return true;
    }

    public static void test2(){
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("2");
        myArrayList.add("3");
        myArrayList.add("4");
        myArrayList.add("5");
        myArrayList.remove("3");
        System.out.println(myArrayList);
    }

    public String get(int index){
        if(index < 0 || index > size){
            throw new ArrayIndexOutOfBoundsException("index: " + index + ",size: " + size);
        }
        return data[index];
    }

    public static void test3(){
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("2");
        myArrayList.add("4");
        myArrayList.add("6");
        myArrayList.add("8");
        myArrayList.add("10");
        System.out.println(myArrayList.get(100));
    }

    public void set(int index,String elem){
        if(index < 0 || index >= size){
            throw new ArrayIndexOutOfBoundsException("index: " + index + ",size: " + size);
        }
        data[index] = elem;
    }

    public static void test4(){
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("3");
        myArrayList.add("5");
        myArrayList.add("9");
        myArrayList.add("5");
        myArrayList.add("7");
        myArrayList.set(3,"10");
        System.out.println(myArrayList);

    }

    public void clear(){
        size = 0;
    }

    public int indexOf(String elem){
        for(int i=0;i< size;i++){
            if(data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }

    public int lastIndexOf(String elem){
        for(int i=size-1;i>= 0;i--){
            if(data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }

    public MyArrayList subList(int fromIndex,int toIndex){
        if(fromIndex < 0 || toIndex > size || fromIndex > toIndex){
            throw new ArrayIndexOutOfBoundsException("输入下标不正确");
        }
        MyArrayList sublist = new MyArrayList(toIndex - fromIndex);
        for(int i=fromIndex;i< toIndex;i++){
            sublist.add(get(i));
        }
        return sublist;

    }

    public static void test5(){
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("5");
        myArrayList.add("5");
        myArrayList.add("9");
        myArrayList.add("10");
        myArrayList.add("3");
        System.out.println(myArrayList.subList(2,4));
    }






    public static void main(String[] args) {
        //test();
        //test2();
        //test3();
        //test4();
        test5();
    }
}
